
import { setToken, getToken, removeToken } from '@/utils/auth'
import { login, getProfile, getUserDetailById } from '@/api/user.js'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state() {
    return {
      token: getToken() || null,
      userInfo: {}
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    setuserInfo(stata, value) {
      stata.userInfo = value
    },
    removeToken(stata, token) {
      stata.token = null
      removeToken()
    }
  },
  actions: {
    async userLogin(store, data) {
      const res = await login(data)
      console.log(res)
      store.commit('setToken', res.data)
    },
    async userLoginout(store) {
      // 1.清空token
      store.commit('removeToken')
      // 2.删除用户信息
      store.commit('setuserInfo', {})
      resetRouter()
    },
    async getProfile(store) {
      const res = await getProfile()
      const resp = await getUserDetailById(res.data.userId)
      console.log(resp)
      store.commit('setuserInfo', { ...resp.data, ...res.data })
    }
  }
}
